package com.automannn.practice.mybatis.sqlSource;

import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.scripting.xmltags.*;
import org.apache.ibatis.session.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenkh
 * @time 2021/11/10 16:13
 */
public class DynamicSqlSourceTest {
    public static void main(String[] args) {
        //动态语句解析
//        Configuration configuration = new Configuration();
//        SqlNode conditionSqlNode = new StaticTextSqlNode("and pid = #{pid}");
//        SqlNode whereSqlNode = new WhereSqlNode(configuration,conditionSqlNode);
//        SqlNode baseSqlNode  = new StaticTextSqlNode("select * from person ");
//        List<SqlNode> sqlNodeList = new ArrayList<>();
//        sqlNodeList.add(baseSqlNode);
//        sqlNodeList.add(whereSqlNode);
//        SqlNode mixedSqlNode = new MixedSqlNode(sqlNodeList);

//        DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(configuration,mixedSqlNode);
//
//
//
//        BoundSql boundSql= dynamicSqlSource.getBoundSql("bbb");

        //动态语句解析过程
        Configuration configuration = new Configuration();
        SqlNode conditionSqlNode1 = new StaticTextSqlNode("and pid = #{pid}");
        SqlNode whereSqlNode1 = new WhereSqlNode(configuration,conditionSqlNode1);


        SqlNode conditionSqlNode2 = new StaticTextSqlNode("and pname = #{pname}");
        SqlNode whereSqlNode2 = new WhereSqlNode(configuration,conditionSqlNode2);

        List<SqlNode> ifSqlNodes = new ArrayList<>();
        SqlNode ifSqlNode1 = new IfSqlNode(whereSqlNode1,"_paramter == null");
        SqlNode ifSqlNode2 = new IfSqlNode(whereSqlNode2,"_paramter != null");

        ifSqlNodes.add(ifSqlNode1);
        ifSqlNodes.add(ifSqlNode2);

        SqlNode defaultSqlNode = new StaticTextSqlNode(" where pid = #{pid} and pname = #{pname}");

        SqlNode chooseSqlNode = new ChooseSqlNode(ifSqlNodes,defaultSqlNode);

        SqlNode baseSqlNode  = new StaticTextSqlNode("select * from person ");

        SqlNode bindNode = new VarDeclSqlNode("myCustomName","'%'+ _parameter +'%'");

        List<SqlNode> sqlNodeList = new ArrayList<>();
        sqlNodeList.add(baseSqlNode);
        sqlNodeList.add(chooseSqlNode);
        sqlNodeList.add(bindNode);
        SqlNode mixedSqlNode = new MixedSqlNode(sqlNodeList);
        //xxx: 动态sql语句，根据xml中解析好的 sqlNode进行构建
        DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(configuration,mixedSqlNode);

        //xxx: 构建动态语句上下文参数测试
        MapperMethod.ParamMap<Object> paramMap = new MapperMethod.ParamMap<>();
        /*xxx: 解析后的动态参数，结果可以分为两大条件进行讨论： 1.是否单参数   2.是否有注解
        *  有注解的情况，统统是 Map结构
        *  单参数，直接返回对象本身，
        *       如果对象本身为 基本数据类型，String，时间日期等等参数，ognl任意取
        *       如果对象本身为 集合类型，也是返回Map, 且存在名称为: collection,list,或者 array 键
        *       如果对象本身为 对象类型， 则可以通过 ognl表达式进行反射获取
        * */
        BoundSql boundSql= dynamicSqlSource.getBoundSql("bbb");

    }


}
